var ajaxCart = {	
	overrideButtons : function()
        {
            $('.cart_quantity_input').typeWatch({highlight: true, wait: 600, captureLength: 0, callback: ajaxCart.update});
            $('.cart_delete').click(function(){                
                ajaxCart.remove($(this).attr('href'),0);
                return false;
            });
        },        
        update: function(val)
        {   
            var rowid = $(this.el).attr('name');
            if (val==0)
            {
                ajaxCart.remove(rowid,0);
                return;
            }
            var exp = new RegExp("^[0-9]+$");            
            if (exp.test(val) == true)
            {
                $.ajax({
                    type: 'POST',
                    url: SITE_URL + 'shopping/update-cart',
                    data: {
                        'rowid' : rowid,
                        'qty' : val
                    },
                    dataType:"json",
                    success: function(data)                          
                    {     
                        if(data.error_str) alert(data.error_str);
                        else {
                            $('#total_items').html('('+data.total_items+')');                    
                            $('#alltotal').html(data.cart_total);
                            $('#subtotal'+rowid).html(data.subtotal);
                        }
                    }
                });                 
            } else {
                alert('Quantity field have to be the number');
                $('input[name="'+rowid+'"]').val($('#oq_'+rowid).val());
            } 
        },
        remove:function(rowid,qty)
        {            
            $.ajax({
                type: 'POST',
                url: SITE_URL + 'shopping/update-cart',
                data: {
                    'rowid' : rowid,
                    'qty' : qty
                },
                dataType:"json",
                success: function(data)                          
                {   //alert(data);//.total_items+'  '+data.cart_total);
                    $('#total_items').html('('+data.total_items+')');
                    
                    $('#tr_'+rowid).fadeOut('slow', function(){
                        if (data.total_items==0) {
                            $('#alltotal').html(0);
                        }                        
                        else{
                            $('#alltotal').html(data.cart_total);
                            //$('#subtotal'+rowid).html(data.price*data.qty);
                        }
                    });    
                    //$('#tr_'+rowid).hidde();
                }
            });             
        }
};        

$(document).ready(function(){
        //$('#empty_cart').hide();
	ajaxCart.overrideButtons();	
});